﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using AnalisisDeRiesgos.Dal;

namespace AnalisisDeRiesgos.Models.Risk
{
    public class SearchRiskModel
    {
        [DisplayName("Buscar por Nombre")]
        [StringLength(50, ErrorMessage = "El nombre debe ser máximo de 50 caracteres")]
        [RegularExpression(@"^[a-zA-Z0-9 \(\)ñÑ\-.,áéíóúÁÉÍÓÚ]+$", ErrorMessage = "El nombre debe ser solo letras mayusculas o minusculas o numeros")]
        public string Pattern { get; set; }

        public List<SearchRiskInfo> RiskInfo
        {
            get
            {
                return riskInfo;
            }
        }

        private List<SearchRiskInfo> riskInfo;

        public SearchRiskModel()
        {
            SearchRisk();
        }

        public void SearchRisk(String pattern)
        {
            riskanalysisEntities entities = new riskanalysisEntities();

            var risks = from r in entities.risks
                        where r.name.Contains(pattern) && r.enabled
                        select r;

            riskInfo = new List<SearchRiskInfo>();

            foreach (risk item in risks)
            {
                riskInfo.Add(
                    new SearchRiskInfo
                    {
                        RiskId = item.riskId,
                        RiskCode = item.riskCode,
                        Name = item.name
                    });
            }
        }

        public void SearchRisk()
        {
            SearchRisk("");
        }

    }
}